package com.king.student.biz;

import java.util.List;
import java.util.Scanner;

import com.king.student.dao.CourseDao;
import com.king.student.dao.StudentCourseDao;
import com.king.student.dao.StudentDao;
import com.king.student.entity.Course;
import com.king.student.entity.Student;
import com.king.student.entity.StudentCourse;

@SuppressWarnings("resource")
public class StudentController {

	private static Student student;
	
	public static void start(){
		login();
	}
	
	private static void login() {
		Scanner sc = new Scanner(System.in);
		System.out.println("**********学生登录**********");
		System.out.print("**** 学号：");
		String studentID = sc.next();
		System.out.println("**************************");
		student = StudentDao.getStudentByStudentID(studentID);
		
		if(student==null){
			System.out.println("该学生不存在");
			login();
			return ;
		}
		
		System.out.println("欢迎你，"+student.getStudentName()+"同学");
		menu();
	}
	
	private static void menu(){
		System.out.println("********主菜单********");
		System.out.println("***1.选修课程                 ***");
		System.out.println("***2.查询课程                 ***");
		System.out.println("***3.查询成绩                 ***");
		System.out.println("***4.退出系统                 ***");
		System.out.println("*********************");
		
		System.out.print("请输入对应功能的编号：");
		boolean is = false;
		Scanner sc = new Scanner(System.in);
		switch(new Scanner(System.in).nextInt()){
			case 1:
				{
					//1.选修课程
					List<Course> list = StudentCourseDao.getStudentCourseByStudentNot(student.getStudentID());
					if(list.isEmpty()){
						System.out.println("暂无可选修课程");
						break;
					}
					
					System.out.println("可选修课程：");
					for(Course temp:list){
						System.out.println(temp);
					}
					
					System.out.print("请输入要选修的课程编号：");
					String courseID = sc.next();
					
					StudentCourse studentCourse = new StudentCourse();
					studentCourse.setStudentID(student.getStudentID());
					studentCourse.setCourseID(courseID);
					is = StudentCourseDao.addStudentCourse(studentCourse);
					System.out.println("选修"+(is?"成功":"失败"));
				}
				break;
			case 2:
				{
					//2.查询课程 
					System.out.println("你选修了以下课程：");
					List<StudentCourse> list = StudentCourseDao.getStudentCourseByStudentID(student.getStudentID());
					for(StudentCourse temp : list){
						System.out.println(CourseDao.getCourseByCourseID(temp.getCourseID()));
					}
				}
				break;
			case 3:
				{
					//3.查询成绩
					System.out.println("以下是你选修课程的成绩：");
					List<StudentCourse> studentCourseList = StudentCourseDao.getStudentCourseByStudentID(student.getStudentID());
					for(StudentCourse temp:studentCourseList){
						System.out.println(CourseDao.getCourseByCourseID(temp.getCourseID())+", 成绩："+temp.getGrade());
					}
				}
				break;
			case 4:
				System.out.println("Good Bye!");
				System.exit(0);
			default:
				menu();
				break;
		}
		menu();
	}
	
}
